<?php
class ModelCatalogGallery extends Model {
	public function addGallery($data) {
      	$this->db->query("INSERT INTO " . DB_PREFIX . "banner SET gallery_name = '" . $this->db->escape($data['gallery_name']) . "',gallery_weblink = '" . $this->db->escape($data['gallery_weblink']) . "',gallery_text = '" . $this->db->escape($data['gallery_text']) . "', ordering = '" . (int)$data['ordering'] . "',gallery_image = '" . $this->db->escape($data['gallery_image']) . "',gallery_type= '" . (int)$data['gallery_type'] . "' ");

		$this->cache->delete('banner');
	}
	
	public function editGallery($gallery_id, $data) {
      	$this->db->query("UPDATE " . DB_PREFIX . "banner SET gallery_name = '" . $this->db->escape($data['gallery_name']) . "',gallery_weblink = '" . $this->db->escape($data['gallery_weblink']) . "',gallery_text = '" . $this->db->escape($data['gallery_text']) . "', ordering = '" . (int)$data['ordering'] . "', gallery_type = '" . (int)$data['gallery_type'] . "' WHERE gallery_id = '" . (int)$gallery_id . "'");

		if (isset($data['gallery_image'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "banner SET gallery_image = '" . $this->db->escape($data['gallery_image']) . "' WHERE gallery_id = '" . (int)$gallery_id . "'");
		}
		$this->cache->delete('banner');
	}
	
	public function deleteGallery($gallery_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "banner WHERE gallery_id = '" . (int)$gallery_id . "'");
		$this->cache->delete('banner');
	}	
	
	public function getGallery($gallery_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "banner WHERE gallery_id = '" . (int)$gallery_id . "'");
		
		return $query->row;
	}
	
	public function getGallerys($data = array()) {
		if ($data) {
			$sql = "SELECT * FROM " . DB_PREFIX . "banner ";
			
			$sort_data = array(
				'gallery_name',
				'ordering'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY gallery_name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					

				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}				
			
			$query = $this->db->query($sql);
		
			return $query->rows;
		} else {
			$gallery_data = $this->cache->get('banner');
		
			if (!$gallery_data) {
				$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "banner ORDER BY gallery_name");
	
				$gallery_data = $query->rows;
			
				$this->cache->set('banner', $gallery_data);
			}
		 
			return $gallery_data;
		}
	}
    public function getTotalGallerys() {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "banner");
		
		return $query->row['total'];
	}
}
?>